<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="department-header"
            >
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 主体结构区域 -->
            <el-tree
              default-expand-all
              :data="treeData"
              :props="defaultProps"
              @node-click="handleNodeClick"
            >
              <template slot-scope="{ data }">
                <el-row
                  type="flex"
                  justify="space-between"
                  style="height: 50px; width: 100%"
                  align="middle"
                >
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="addDept(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="editDept(data.id)"
                            >编辑子部门</el-dropdown-item>
                            <!-- 递归用这个方法 -->
                            <el-dropdown-item
                              v-if="data&&!data.children"
                              @click.native="delDept(data.id)"
                            >删除子部门</el-dropdown-item>
                            <!-- 筛选器用这个方法 -->
                            <!-- <el-dropdown-item
                              v-if="data&&data.children.length===0"
                              @click.native="delDept(data.id)"
                            >删除子部门</el-dropdown-item> -->
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <!-- 添加或编辑弹框 -->
        <el-dialog
          :title="isEdit ? '编辑部门' : '添加部门'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 添加子组件弹层 -->
          <deptDialog
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @close="updateDepart"
            @closeDialog="showDialog = false"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import deptDialog from './deptDialog.vue'
import { getDepartments, delDepartment } from '@/api/departments'
import { transListTree } from '@/utils/index'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 tabs
      // 树行控件需要渲染的数据
      treeData: [
        {
          name: '财务部',
          children: [
            {
              name: '财务核算部',
              children: [
                {
                  name: '核算 1 部'
                }
              ]
            },
            {
              name: '财务稽查部',
              children: [
                {
                  name: '稽查 1 部'
                }
              ]
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 控制添加或编辑弹框展示
      parentId: '',
      isEdit: false,
      originList: []
    }
  },

  created() {
    this.getDepartments()
  },
  methods: {
    editDept(id) {
      // console.log(id)
      this.parentId = id
      this.isEdit = true
      this.showDialog = true
    },
    async delDept(id) {
      const confirmRes = await this.$confirm(
        '此操作将永久删除该部门, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (confirmRes === 'cancel') return this.$message('您取消了删除')
      const res = await delDepartment(id)
      // 重新获取组织架构的数据
      this.getDepartments()
      // 给用户删除提示
      this.$message.success(res.message)
    },
    updateDepart() {
      this.showDialog = false
      this.getDepartments()
    },

    // 添加部门
    addDept(id) {
      this.parentId = id
      this.isEdit = false
      this.showDialog = true
    },

    // 当树行节点被点击时触发
    handleNodeClick() {},

    async getDepartments() {
      const res = await getDepartments()
      // console.log(transTree(res.data.depts))
      // this.treeData = transListTree(res.data.depts, '')
      const originList = res.data.depts.map(item => ({
        id: item.id,
        name: item.name,
        pid: item.pid,
        code: item.code
      }))
      this.originList = originList

      this.treeData = transListTree(res.data.depts, '')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  margin-top: 10px;
}
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
